package howl.adaptor;

import java.util.ArrayList;
import java.util.List;

import compiler.exceptions.HowlModelException;
import compiler.nonterminals.CreateStmt;
import compiler.nonterminals.DeleteStmt;
import compiler.nonterminals.InsertStmt;
import compiler.terminals.DataValue;

import Pachube.Feed;
import howl.Twitter.TwitterProvider;
import howl.types.Datum;
import howl.types.Entry;
import howl.types.HowlUser;
import howl.types.Insert;
import howl.types.TableDescriptor;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.http.AccessToken;

public class Yfd extends SimplexAdaptor {

	public Yfd(HowlUser u) {
		super(u);
	}

	@Override
	public boolean put(String j) {
		return false;
	}

	@Override
	public void doCreate(CreateStmt ht) {
		// TODO Auto-generated method stub

	}

	@Override
	public void doDelete(DeleteStmt ht) {
		// TODO Auto-generated method stub

	}

	@Override
	public void doInsert(InsertStmt ht) {
		Insert ins = new Insert();
		TableDescriptor td;
		try {
			td = this.model.getTableDescriptor(ht.getFeedname().getFeedname());

			if (td != null) {

				List<DataValue> vals = ht.getDatavalues();

				Entry e = new Entry(td, ht.getDataprovider().getName());
				ins.add(e);

				Datum d;

				for (int j = 0; j < vals.size(); j++) {
					DataValue dv = vals.get(j);
					d = new Datum(td, e, dv.getName(), dv.getValue()
							.doubleValue());
					ins.add(d);
				}

				this.doInsert(ins);
			} else {
				// TODO ERROR
			}
		} catch (HowlModelException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

	}

	private boolean doInsert(Insert j) {
		if (j instanceof Insert) {
			try {
				TableDescriptor td = null;
				Insert i = (Insert) j;
				ArrayList l = (ArrayList) i.getItems();
				if (l.size() > 0) {
					if (l.get(0) instanceof Entry) {
						td = this.model.getTableDescriptor(((Entry) l.get(0))
								.getParentID());
					} else if (l.get(0) instanceof Datum) {
						td = this.model.getTableDescriptor(((Datum) l.get(0))
								.getGrandParentID());
					}

					Datum d = null;
					for (int k = 0; k < l.size(); k++) {
						if (l.get(k) instanceof Datum) {
							d = ((Datum) l.get(k));
							this.postYFD(td.getName(), d.getName(), d
									.getValue());
						}
					}
				}
			} catch (HowlModelException e) {
				e.printStackTrace();
			}
		}
		return false;
	}

	private void postYFD(String entity, String field, Double value) {

		try {
			Twitter t = TwitterProvider.getTwitter();

			AccessToken a = new AccessToken(this.user.getTwitterToken(),
					this.user.getTwitterTokenSecret());

			t.setOAuthAccessToken(a);
			String status = " " + entity + "." + field + " "
					+ value.toString();
			t.sendDirectMessage("yfd", status);
			//t.updateStatus(status);

		} catch (TwitterException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public static void followYFD(HowlUser hu) {

		try {
			Twitter t = TwitterProvider.getTwitter();

			AccessToken a = new AccessToken(hu.getTwitterToken(), hu
					.getTwitterTokenSecret());

			t.setOAuthAccessToken(a);
			String status = "follow @yfd";
			t.updateStatus(status);

		} catch (TwitterException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
